﻿Imports System.Data.SqlClient
Imports System.Data
Imports System.Web.Services

Public Class WebForm1
    Inherits System.Web.UI.Page
    Dim myConnection As SqlConnection
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        myConnection = New SqlConnection("Data Source= (local);" _
                                         & "Database=Phonebook;Integrated Security=True")
        User.Text = CType(Session.Item("user"), String)
        'GridViewPreRender()
    End Sub

    'Sub ValidateBtn_OnClick(sender As Object, e As EventArgs) Handles Button2.Click
    '    ' Display whether the page passed validation.
    '    If Page.IsValid Then
    '        Validation.Text = "Page is valid."
    '    Else
    '        Validation.Text = "Page is not valid!"
    '    End If
    'End Sub

    Protected Sub Search_Click(sender As Object, e As EventArgs) Handles Search.Click
        GridView1.DataSourceID = "SqlDataSource2"
    End Sub

    Protected Sub Add_Click(sender As Object, e As EventArgs) Handles Add.Click
        Response.BufferOutput = True
        Response.Redirect("AddNewContact.aspx")

    End Sub

    'Protected Sub Delete_Click(sender As Object, e As EventArgs) Handles Delete.Click
    '    Dim deleteSql As SqlCommand
    '    Dim deleteCmd As String
    '    deleteCmd = "delete from contacts where cell = @cell"
    '    deleteSql = New SqlCommand(deleteCmd, myConnection)
    '    deleteSql.Parameters.Add(New SqlParameter("@cell", SqlDbType.NChar, 50))
    '    deleteSql.Parameters("@cell").Value = CellDelete.Text
    '    Try
    '        myConnection.Open()
    '        deleteSql.ExecuteNonQuery()
    '        Message.ForeColor = Drawing.Color.Green
    '        Message.Text = "Delete Executed"
    '    Catch ex As Exception

    '    End Try
    '    myConnection.Close()
    '    GridView1.DataSourceID = "SqlDataSource1"
    'End Sub

    'Protected Sub Edit_Click(sender As Object, e As EventArgs) Handles Edit.Click
    '    Session("cell") = CellEdit.Text
    '    Response.BufferOutput = True
    '    Response.Redirect("EditContact.aspx")
    '    'Server.Transfer("EditContact.aspx", True)
    'End Sub

    Protected Sub ImageMap1_Click(sender As Object, e As System.Web.UI.WebControls.ImageMapEventArgs) Handles ImageMap1.Click
        Dim postback As String
        postback = e.PostBackValue
        ImageMapLabel.Text = "You clicked the " & postback
    End Sub

    Protected Sub BulletedList1_Click(sender As Object, e As System.Web.UI.WebControls.BulletedListEventArgs) Handles BulletedList1.Click
        Dim index As Integer = e.Index
        Dim clickedItem As ListItem = BulletedList1.Items(index)
        BulletedListLabel.Text = clickedItem.Value

    End Sub

    Protected Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        ImageMapLabel.Text = "Image Checkbox Checked Changed"
    End Sub

    Protected Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
        ImageMapLabel.Text = "ImageMap Checkbox Checked Changed"
    End Sub

    Protected Sub CheckBoxList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckBoxList1.SelectedIndexChanged
        Dim selectedItem = CheckBoxList1.SelectedItem
        If Not selectedItem Is Nothing Then
            CheckBoxListLabel.Text = selectedItem.Value
        Else
            CheckBoxListLabel.Text = "Nothing is selected"
        End If
    End Sub

    Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList1.SelectedIndexChanged
        DropDownListLabel.Text = DropDownList1.SelectedValue
    End Sub

    Protected Sub ImageButton1_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        ImageMapLabel.Text = "You clicked an image button"
    End Sub

    Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
        Message.Text = GridView1.SelectedValue

    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        GridView1.DataSourceID = "SqlDataSource1"
    End Sub

    Protected Sub GridViewAdd(sender As Object, e As EventArgs)
        Dim id As Integer = 0
        Dim mySqlCommand As SqlCommand
        Dim selectCommand As String
        selectCommand = "select count(*) from contacts;"
        mySqlCommand = New SqlCommand(selectCommand, myConnection)
        Try
            myConnection.Open()
            Dim reader As SqlDataReader = mySqlCommand.ExecuteReader()
            reader.Read()
            id = reader.GetInt32(0) + 1

        Catch ex As Exception

        End Try
        'SqlDataSource1.InsertCommand = "INSERT INTO contacts(fname, lname, cell, picture, id) VALUES (Null, 'lname', Null, '~/Images/Tulips.jpg'," & id & ")"
        SqlDataSource1.InsertCommand = "INSERT INTO contacts(fname, lname, cell, picture) VALUES (Null, 'lname', Null, '~/Images/Tulips.jpg')"
        SqlDataSource1.Insert()
        myConnection.Close()
    End Sub

    Protected Sub GridViewPreRender()
        For index As Integer = 1 To GridView1.Rows.Count - 1
            GridView1.Rows(index).Cells(6).Controls(1).Visible = False
        Next

    End Sub

    Protected Sub CustomValidator1_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
        args.IsValid = If(CheckBoxList1.SelectedItem Is Nothing, False, True)
    End Sub

    'Protected Sub FormViewInsert_Click(sender As Object, e As EventArgs)
    '    Dim id As Integer = 0
    '    Dim mySqlCommand As SqlCommand
    '    Dim selectCommand As String
    '    selectCommand = "select count(*) from contacts;"
    '    mySqlCommand = New SqlCommand(selectCommand, myConnection)
    '    Try
    '        myConnection.Open()
    '        Dim reader As SqlDataReader = mySqlCommand.ExecuteReader()
    '        reader.Read()
    '        id = reader.GetInt32(0) + 1

    '    Catch ex As Exception

    '    End Try
    '    SqlDataSource1.InsertCommand = "INSERT INTO contacts(fname, lname, cell, picture, id) VALUES (@fname, @lname, @cell, '~/Images/Tulips.jpg'," & id & ")"
    '    'SqlDataSource1.InsertCommand = "INSERT INTO contacts(fname, lname, cell, picture, id) VALUES (Null, 'lname', Null, '~/Images/Tulips.jpg'," & id & ")"
    '    'SqlDataSource1.Insert()
    '    myConnection.Close()
    'End Sub

    'Protected Sub DetailsViewInsert_Click(sender As Object, e As EventArgs)
    '    Dim id As Integer = 0
    '    Dim mySqlCommand As SqlCommand
    '    Dim selectCommand As String
    '    selectCommand = "select count(*) from contacts;"
    '    mySqlCommand = New SqlCommand(selectCommand, myConnection)
    '    Try
    '        myConnection.Open()
    '        Dim reader As SqlDataReader = mySqlCommand.ExecuteReader()
    '        reader.Read()
    '        id = reader.GetInt32(0) + 1

    '    Catch ex As Exception

    '    End Try
    '    SqlDataSource1.InsertCommand = "INSERT INTO contacts(fname, lname, cell, picture, id) VALUES (@fname, @lname, @cell, '~/Images/Tulips.jpg'," & id & ")"
    '    'SqlDataSource1.InsertCommand = "INSERT INTO contacts(fname, lname, cell, picture, id) VALUES (Null, 'lname', Null, '~/Images/Tulips.jpg'," & id & ")"
    '    'SqlDataSource1.Insert()
    '    myConnection.Close()
    'End Sub


    'Protected Sub ValidateTextBox_Click(sender As Object, e As EventArgs) Handles ValidateTextBox.Click
    '    If Page.IsValid Then
    '        Validation.Text = "Page is valid."
    '    Else
    '        Validation.Text = "Page is not valid!"
    '    End If
    'End Sub


    Protected Sub EvenNumberValidationServer(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator2.ServerValidate
        args.IsValid = If(Convert.ToInt32(EvenNumber.Text) Mod 2, False, True)

    End Sub


    'Protected Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    '    Time.Text = System.DateTime.Now.ToString
    'End Sub

    'shared is equivalent to static
    <WebMethod()> _
    Public Shared Function HelloWorld(ByVal yourName As String) As String
        Return String.Format("Hello {0}", yourName)
    End Function
End Class